<?php
//构建web服务器
$http = new Swoole\Http\Server('0.0.0.0', 9501);

$http->set([
    'worker_num'=>1,
    'document_root'=>'/www/wwwroot/swoole/rpc',  //虚拟目录指向的位置 只针对静态资源 绝对路径
    'enable_static_handler'=>true
]);

$http->on('Request', function ($request, $response) {
    //请求的文件名
    $req_file = $request->server['request_uri'];
    //真实的文件路径
    $filepath = __DIR__.'/rpc/'.$req_file;
    //状态码
    $status = 404;
    //返回数据
    $html = json_encode(['status'=>1000,'msg'=>'没有数据']);
//判断文件是否存在
    if (file_exists($filepath)){
        //封装
        $_GET = $request->get;
        $_POST = $request->post;
        $_FILES = $request->files;
        $status = 200;
        ob_start();
        include  $filepath;
        //读取暂存区
        $html = ob_get_contents();
        ob_clean();
    }
    $response->status($status);
    $response->header('Content-Type', 'text/html; charset=utf-8');
    $response->end($html);
});

$http->start();
